// pages/fenhong/fenhong.js
const app = getApp();
var allList = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    article: {},
    page: 1,
    uid: "",
    isFenHong: "",
    emptyMember: '../image/not-member-empty.png',
    memberType: 10,
    memberList: [],
    isLoad: false,
  },
  // 获取分红规则
  getRule() {
    let that = this
    wx.request({
      url: 'https://api.bshlsqs.com/about/rule',
      success: function(res) {
        console.log(res)
        let data = app.towxml.toJson(res.data.data.body, 'html');

        that.setData({
          article: data
        });

      },
      fail: function(res) {},

    })
  },
  // 切换tab
  onChange(event) {
    console.log(event)
    var index = event.detail.index;
    this.setData({
      active: index
    })
  },
  // 加载成员
  loadFans() {
    let that = this;
    wx.getStorage({
      key: 'uid',
      success: function(res) {
        console.log(res)
        that.setData({
          uid: res.data
        })
        wx.request({
          url: 'https://api.bshlsqs.com/user_center/fans_list',
          data: {
            uid: that.data.uid,
            p: that.data.page
          },
          success(res) {
            console.log(res)
            var list = res.data.data.data.list;
            var pager = res.data.data.pager;

            for (var i = 0; i < list.length; i++) {
              allList.push(list[i])
            }

            if (pager.nowPage == pager.totalPages) { // 只剩下一页
              that.setData({
                memberList: allList
              })
            } else if (pager.nowPage < pager.totalPages) { // 多页，请求完后统一塞进去
              var nextP = that.data.p + 1;
              that.setData({
                p: nextP
              })
              that.loadList(cate)
            }

            that.setData({
              isFenHong: res.data.data.member,
              memberType: res.data.data.member,
              isLoad: true
            })
          }
        })
      },
    })
  },

  // 提交申请
  handleApply() {
    var applyType = "applyType"

    wx.navigateTo({
      url: `/pages/index/index?applyType=${applyType}`,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getRule()
    this.loadFans()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var invite_code = "";
    wx.getStorage({
      key: 'invite_code',
      success: function(res) {
        invite_code: res.data
      },
    })
    return {
      path: `/pages/newRegister/newRegister?invite_code=${invite_code}`
    }
  }
})